<?php
/**
 * Description of NewsController
 * @author : Nguyễn Duy Cường
 * @since Oct 19, 2012
 */
class CmtController extends AppController {

    public $name = 'Cmt';
    public $uses = array('Cmt');


    public function beforeFilter() {
        parent::beforeFilter();
        $this->cklogin();
    }

    public function binhluan($id = null, $j = null){
        $this->autoLayout = false;
        $this->autoRender = false;
        
        if(!empty($this->request->data)){
            if(empty($id) || empty($j)){
                if(isset($this->notice['not_exits'])) $this->Session->setFlash($this->notice['not_exits'], 'default', array('class' => 'alert alert-error'));
                $this->redirect(DOMAIN);
                exit;
            }
            $this->loadModel('Product');
            $dataproduct = $this->Product->findById($id);
            if(empty($dataproduct) || empty($j)){
                if(isset($this->notice['not_exits'])) $this->Session->setFlash($this->notice['not_exits'], 'default', array('class' => 'alert alert-error'));
                $this->redirect(DOMAIN);
                exit;
            }
            if(empty($this->request->data['Cmt']['content'])){
                if(isset($this->notice['not_exits'])) $this->Session->setFlash($this->notice['not_exits'], 'default', array('class' => 'alert alert-error'));
                $this->redirect(DOMAIN);
                exit;
            }
            if(!empty($_GET['traloi'])){
                $datap = $this->Cmt->find('first', array(
                    'fields' => array('id', 'foreign_id'),
                    'conditions'=>array(
                        'Cmt.status' => 1,
                        'Cmt.comment_id' => null,
                        'Cmt.id' => $_GET['traloi'],
                        'Cmt.foreign_id' => $id
                    )
                ));
                if(empty($datap)){
                    if(isset($this->notice['not_exits'])) $this->Session->setFlash($this->notice['not_exits'], 'default', array('class' => 'alert alert-error'));
                    $this->redirect(DOMAIN);
                    exit;
                }
                $this->request->data['Cmt']['comment_id'] = $_GET['traloi'];
            }
            
            $this->request->data['Cmt']['foreign_id'] = $id;
            $this->request->data['Cmt']['user_id'] = $this->user_login['id'];
            $this->request->data['Cmt']['type'] = $j;
            
            //su ly cong slg comment vao user fields sum_cmt
            $find_post_user = $this->Product->find('first', array(
                'fields' => array('id', 'user_member'),
                'conditions' => array(
                    'Product.type_member' => 'type_member',
                    'Product.user_member <>' => null,
                    'Product.id' => $id
                )
            ));
            if(!empty($find_post_user)){
                $this->loadModel('User');
                $find_user = $this->User->findById($find_post_user['Product']['user_member']);
                if(!empty($find_user)){
                    $this->User->id = $find_post_user['Product']['user_member'];
                    $this->User->saveField('sum_cmt', ($find_user['User']['sum_cmt'] + 1));
                }
            }
            
            if($this->Cmt->save($this->request->data['Cmt'])){
                //xu ly + them cmt trong bai viet
                $this->Product->id = $this->request->data['Cmt']['foreign_id'];
                $post = $this->Product->findById($this->request->data['Cmt']['foreign_id']);
                $this->Product->saveField('cmt', ($post['Product']['cmt'] + 1));           
                
                if(isset($this->notice['add_success'])) $this->Session->setFlash($this->notice['add_success'], 'default', array('class' => 'alert alert-success'));
                $this->redirect($this->referer());
                exit;
            }
            if(isset($this->notice['add_fail'])) $this->Session->setFlash($this->notice['add_fail'], 'default', array('class' => 'alert alert-error'));
            $this->redirect(DOMAIN);
            exit;
            
        }
        if(isset($this->notice['not_exits'])) $this->Session->setFlash($this->notice['not_exits'], 'default', array('class' => 'alert alert-error'));
        $this->redirect(DOMAIN);
        exit;
    }
}
